package com.itheima.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 配置服务器启动时,需要注册的过滤器
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        FilterRegistration.Dynamic dynamic =
                servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
        dynamic.setInitParameter("encoding","utf-8");
        //设置需要过滤器,过滤的路径
        // 参数l: 拦截方式
        // 参数2: 加载顺序, 和web.xml文件相比,先加载谁
        // 参数3: 拦截的路径
        dynamic.addMappingForUrlPatterns(null,true,"/*");
    }

    /**
     * 加载初始配置类,创建Spring和Mybatis的环境
     *      监听器
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class,MybatisConfig.class};
    }

    /**
     * 加载SpringMVC的配置类
     *          DispatcherServlet
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    /**
     * 前端控制器需要拦截的路径
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
